import lodash from 'lodash';
import ElementPlus from 'unplugin-element-plus/vite';

const virtualFile = '@virtual-file';
const virtualId = '\0' + virtualFile;
const nestedVirtualFile = '@nested-virtual-file';
const nestedVirtualId = '\0' + nestedVirtualFile;

export default async function plugins(params) {
  let a = [
    {
      name: 'vite-plugin-transform-index-html',
      transformIndexHtml: {
        order: 'pre',
        handler(html) {
          return lodash.template(html)({
            ___ENTRY___: 'src/main.js',
            ...params.define,
          });
        },
      },
    },
    {
      name: 'virtual',
      resolveId(id) {
        if (id === '@foo') {
          return id;
        }
      },
      load(id, options) {
        const ssrFromOptions = options?.ssr ?? false;
        if (id === '@foo') {
          // Force a mismatch error if ssrBuild is different from ssrFromOptions
          return `export default { msg: '${
            command === 'build' && !!ssrBuild !== ssrFromOptions
              ? `defineConfig ssrBuild !== ssr from load options`
              : 'hi'
          }' }`;
        }
      },
    },
    {
      name: 'virtual-module',
      resolveId(id) {
        if (id === virtualFile) {
          return virtualId;
        } else if (id === nestedVirtualFile) {
          return nestedVirtualId;
        }
      },
      load(id) {
        if (id === virtualId) {
          return `export { msg } from "@nested-virtual-file";`;
        } else if (id === nestedVirtualId) {
          return `export const msg = "[success] from conventional virtual file"`;
        }
      },
    },
    {
      name: 'vite-plugin-build',
      apply: 'build',
      enforce: 'post',
      config(v, o) {},
      configResolved(v) {
        return v;
      },
      options(v) {
        return v;
      },
      buildStart(v) {
        return v;
      },
      buildEnd(v) {
        return v;
      },
      closeBundle() {},
      // transform(code, id, options) {},
    },
    ElementPlus(),
  ];

  return a;
}
